package com.java.escape.computeAndListOrInterface;

import java.util.*;

/**
 * @ClassName LambdaProblem
 * @Description Lambda表达式的使用
 * @Author wyp
 * @Date 2023/8/25 10:21
 * @Version 1.0
 */
@SuppressWarnings("all")
public class LambdaProblem {

    private static final Map<Long, Worker> workerMap = new HashMap<>();

    static {
        workerMap.put(1L, new Worker("xxx", "dasdad", 22));
    }

    /**
     * java 1.8 之前创建线程
     */
    public static void baseUse() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Anonymous Class thread run");
            }
        }).start();
    }

    /**
     * java8 创建线程
     */
    public static void lambdaUse() {
        new Thread(() -> System.out.println("Lambda use")).start();
    }

    public static void main(String[] args) {
        baseUse();
        lambdaUse();
        compareLength();
        compareUseJava8();
        IFindWorker iFindWorker = id -> workerMap.get(id);
        IFindWorker iFindWorker2 = workerMap::get;
        System.out.println(iFindWorker.getWorker(1L));
        System.out.println(iFindWorker2.getWorker(1L));
        badUseLambda();
        maybeNiceUsage();
    }

    /**
     * 集合字符串长度排序  Java 8之前的写法
     */
    public static void compareLength() {
        List<String> list = Arrays.asList("x", "yzx", "z", "dasdas");
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String s1, String t1) {
                if (s1 == null) {
                    return -1;
                }
                if (t1 == null) {
                    return 1;
                }
                return s1.length() - t1.length();
            }
        });
        System.out.println(list);
    }

    /**
     * 集合字符串长度排序  Java 8的写法
     * lambda 只能只用在只有一个抽象方法的函数式接口里面使用
     */
    public static void compareUseJava8() {
        List<String> list = Arrays.asList("x", "yxz", "z", "dasdas");
        Collections.sort(list, (s1, t1) -> {
            if (s1 == null) {
                return -1;
            }
            if (t1 == null) {
                return 1;
            }
            return s1.length() - t1.length();
        });
        System.out.println(list);

    }

    /**
     * Stream 和 Lambda 可能导致计算低效
     */
    public static void badUseLambda() {
        List<String> list = Arrays.asList("qinsdasdas", "qsad1", "dsadadsad");
        int i = list.stream().filter(s -> s.startsWith("q"))
                .mapToInt(String::length)
                .max()
                .getAsInt();
        System.out.println(i);
    }

    /**
     * 在面对用户场景不同时，选择合适的方法
     */
    public static void maybeNiceUsage() {
        List<String> list = Arrays.asList("qinsdasdas", "qsad1", "dsadadsad");
        int longCount = 0;
        for (String s : list) {
            if (s.startsWith("q") && s.length() > longCount) {
                longCount = s.length();
            }
        }
        System.out.println(longCount);
    }
}
